/*
 * \brief  C-library back end
 * \author Norman Feske
 * \date   2008-11-11
 */

/*
 * Copyright (C) 2008-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#include <base/env.h>
#include <sys/mman.h>

extern "C" int raw_write_str(const char *s);

static void print_hex_int(unsigned int value)
{
	for (unsigned i = 8*sizeof(value); i; i -= 4) {
		char buf[2] = {0, 0};
		buf[0] = "0123456789abcdef"[(value >> (i - 4)) & 0xf];
		raw_write_str(buf);
	}
}

extern "C" void wait_for_continue();

extern "C" void *mmap(void *start, size_t length, int prot, int flags,
                      int fd, off_t offset)
{
	if (!start && fd == -1) {

		raw_write_str("mmap called for anonymous memory, allocating new ram dataspace\n");

		using namespace Genode;

		Ram_dataspace_capability ds = env()->ram_session()->alloc(length);
		return env()->rm_session()->attach(ds);

	}

	raw_write_str("mmap called with the following arguments:\n");

	raw_write_str("fd     = 0x");
	print_hex_int(fd);
	raw_write_str("\n");

	raw_write_str("start  = 0x");
	print_hex_int((unsigned int)start);
	raw_write_str("\n");

	raw_write_str("flags  = 0x");
	print_hex_int(flags);
	raw_write_str("\n");

	raw_write_str("length = 0x");
	print_hex_int(length);
	raw_write_str("\n");

	return (void *)-1;
}

